﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace BandwidthMonitor
{
    public class WindowHelper
    {
        public TaskbarCoordinates GetTaskBarLocation()
        {
            TaskbarCoordinates taskBarCoordinates = new TaskbarCoordinates();
            taskBarCoordinates.TaskBarLocation = TaskbarCoordinates.TaskBarPosition.Bottom;

            bool taskBarOnTopOrBottom = (Screen.PrimaryScreen.WorkingArea.Width == Screen.PrimaryScreen.Bounds.Width);

            if (taskBarOnTopOrBottom)
            {
                if (Screen.PrimaryScreen.WorkingArea.Top > 0)
                    taskBarCoordinates.TaskBarLocation = TaskbarCoordinates.TaskBarPosition.Top;
            }
            else
            {
                if (Screen.PrimaryScreen.WorkingArea.Left > 0)
                {
                    taskBarCoordinates.TaskBarLocation = TaskbarCoordinates.TaskBarPosition.Left;
                }
                else
                {
                    taskBarCoordinates.TaskBarLocation = TaskbarCoordinates.TaskBarPosition.Right;
                }
            }

            return taskBarCoordinates;
        }

        public void PositionFormRelativeToTaskbar(Form myForm)
        {
            TaskbarCoordinates taskbar = GetTaskBarLocation();

            switch (taskbar.TaskBarLocation)
            {
                case TaskbarCoordinates.TaskBarPosition.Bottom:
                    myForm.Left = (taskbar.WorkingWidth - myForm.Width - 7);
                    myForm.Top = (taskbar.WorkingHeight - myForm.Height - 7);
                    break;
                case TaskbarCoordinates.TaskBarPosition.Top:
                    myForm.Left = (taskbar.WorkingWidth - myForm.Width - 7);
                    myForm.Top = (Screen.PrimaryScreen.WorkingArea.Top + 7);
                    break;
                case TaskbarCoordinates.TaskBarPosition.Right:
                    myForm.Left = (taskbar.WorkingWidth - myForm.Width - 7);
                    myForm.Top = (taskbar.WorkingHeight - myForm.Height - 7);
                    break;
                case TaskbarCoordinates.TaskBarPosition.Left:
                    myForm.Left = (Screen.PrimaryScreen.WorkingArea.Left + 7);
                    myForm.Top = (taskbar.WorkingHeight - myForm.Height - 7);
                    break;
            }
        }
    }

    public class TaskbarCoordinates
    {
        public enum TaskBarPosition
        {
            Top = 1,
            Bottom = 2,
            Left = 3,
            Right = 4
        }

        public TaskBarPosition TaskBarLocation;

        public int WorkingWidth
        {
            get
            {
                switch (TaskBarLocation)
                {
                    case TaskBarPosition.Bottom:
                    case TaskBarPosition.Top:
                        return Screen.PrimaryScreen.WorkingArea.Width;
                    case TaskBarPosition.Left:
                    case TaskBarPosition.Right:
                        return Screen.PrimaryScreen.Bounds.Width - TaskbarOffset;
                    default:
                        return 0;
                }
            }
        }

        public int WorkingHeight
        {
            get
            {
                switch (TaskBarLocation)
                {
                    case TaskBarPosition.Bottom:
                        return Screen.PrimaryScreen.Bounds.Height - TaskbarOffset;
                    case TaskBarPosition.Top:
                        return Screen.PrimaryScreen.Bounds.Height - TaskbarOffset;
                    case TaskBarPosition.Left:
                        return Screen.PrimaryScreen.WorkingArea.Height;
                    case TaskBarPosition.Right:
                        return Screen.PrimaryScreen.WorkingArea.Height;
                    default:
                        return 0;
                }
            }
        }

        private int TaskbarOffset
        {
            get
            {
                switch (TaskBarLocation)
                {
                    case TaskBarPosition.Bottom:
                    case TaskBarPosition.Top:
                        return Screen.PrimaryScreen.Bounds.Bottom - Screen.PrimaryScreen.WorkingArea.Bottom;
                    case TaskBarPosition.Left:
                        return Screen.PrimaryScreen.Bounds.Left - Screen.PrimaryScreen.WorkingArea.Left;
                    case TaskBarPosition.Right:
                        return Screen.PrimaryScreen.Bounds.Right - Screen.PrimaryScreen.WorkingArea.Right;
                    default:
                        return 0;
                }
            }
        }
    }
}
